<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Setting extends BaseController {
    // 智能门禁配置
    private $schoolSmartLockName = 'school_smart_lock';

	public function __construct() {
        parent::__construct();
        // 后台初始化方法
        $this->admin_init();
        // 用户类型判断
        if($this->userType != USER_TYPE_SCHOOL && $this->userType != USER_TYPE_TEACHER) redirect(admin_url("login"));
        // 加载类库
        $this->load->model('schoolModel');
        $this->load->model('accountModel');
    }

    // 基础设置
	public function index() {
        $this->load->model('configModel');
        // // 智能门锁
        // if(isset($this->rmxSchool['school_smart_lock']) && $this->rmxSchool['school_smart_lock'] == 1) {
        //     // 获取门锁配置
        //     $smartLockConfig = $this->configModel->get_school_campus_config($this->rmxSchool['id'], $this->campusInfo['id'], $this->schoolSmartLockName);
        //     if($smartLockConfig['config_value']) {
        //         $this->smartLockConfigValueArr = json_decode($smartLockConfig['config_value'], true); 
        //     }            
        //     // echo '<pre>';var_export($smartLockConfig);exit;
        // }
        $this->display(ADMIN_DIR. DIRECTORY_SEPARATOR. "setting". DIRECTORY_SEPARATOR. "index");
	}
    // 保存基础设置
    public function save_setting(){
        $this->load->model('configModel');
        $schoolName = $this->post("school_name");
        $campusName = $this->post("campus_name");
        $schoolMotto = trim($this->post("school_motto"));

        // 参数检查
        if(!$schoolName) throw new Exception("请填写学校名称");
        if(!$campusName) throw new Exception("请填写校区名称");

        // 保存学校名称
        $this->rmxUser['school']['school_name'] = $schoolData['school_name'] = $schoolName;
        $this->db->update(SchoolModel::$schoolTable, $schoolData,"id='{$this->rmxSchool['id']}'");
        // // 修改session学校信息
        // $this->session->rmxUser = $this->rmxUser;

        // 保存校区名称
        $this->campusInfo['campus_name'] = $campusData['campus_name'] = $campusName;
        $this->db->update(SchoolModel::$campusTable, $campusData,"id='{$this->campusInfo['id']}' AND `school_id`='{$this->rmxSchool['id']}'");
        // 修改session校区信息
        $this->session->campusInfo = $this->campusInfo;
        // 修改session校区列表
        $params['schoolId'] = $this->rmxSchool['id'];
        $params['isDelete'] = 0;
        $this->session->campusList = $this->schoolModel->get_campus_list($params);

        // 保存校训
        $this->rmxUser['school']['school_motto'] = $schoolData['school_motto'] = $schoolMotto;
        // 是否开启刷卡签到
        $this->rmxUser['school']['enable_card_sign'] = $schoolData['enable_card_sign'] = $enableCardSign;
        $this->db->update(SchoolModel::$schoolTable, $schoolData,"id='{$this->rmxSchool['id']}'");


        // // 智能门锁
        // if(isset($this->rmxSchool['school_smart_lock']) && $this->rmxSchool['school_smart_lock'] == 1) {
        //     $schoolSmartLockToken = trim($this->post("school_smart_lock_token"));
        //     $schoolSmartLockIp = trim($this->post("school_smart_lock_ip"));
        //     $smartLockArr = array(
        //         'token' => $schoolSmartLockToken,
        //         'ip' => $schoolSmartLockIp
        //     );
        //     $smartLockJson = json_encode($smartLockArr, JSON_UNESCAPED_UNICODE);
        //     $this->configModel->save_school_campus_config($this->rmxSchool['id'], $this->campusInfo['id'], $this->schoolSmartLockName, $smartLockJson);
        // }

        // 修改session学校信息
        $this->session->rmxUser = $this->rmxUser;

        exit(json("保存成功", true));
    }

    // 日志管理
	public function log() {
        $this->load->library('logger');
        // 操作类型列表
        $this->actionTypeList = $this->logger->get_action_type_list();

        $this->display(ADMIN_DIR. DIRECTORY_SEPARATOR. "setting". DIRECTORY_SEPARATOR. "log");
    }
    
    // 获取日志列表
    public function get_log_list(){
        // 学生姓名筛选
        if(trim($this->get("keyword"))) $totalParams['keyword'] = $data['keyword'] = trim($this->get("keyword"));
        // 用户类型
        if($this->get("user_type")) $totalParams['userType'] = $data['userType'] = $this->get("user_type");
        // 操作类型
        if($this->get("action_type")) $totalParams['actionType'] = $data['actionType'] = $this->get("action_type");
        if($this->get("begintime")) $totalParams['begintime'] = $data['begintime'] = $this->get("begintime") ? $this->get("begintime") . ':00' : 0;
        if($this->get("endtime")) $totalParams['endtime'] = $data['endtime'] = $this->get("endtime") ? $this->get("endtime") . ':59' : 0;
        // 获取数据条数
        $totalParams['getRowNumber'] = true;
        $totalParams['schoolId'] = $this->rmxSchool['id'];
        // 过滤软删除
        $totalParams['isDelete'] = 0;
        $total = $this->accountModel->get_log_list($totalParams);
        // 获取数据
        $data['pageStart'] = $this->get('start');
        $data['pageSize']= $this->get("length");
        $data['schoolId'] = $this->rmxSchool['id'];
        // 过滤软删除
        $data['isDelete'] = 0;
        // 按id倒序
        $data['orderBy'] = '`createtime` DESC';
        $logList = $this->accountModel->get_log_list($data);
  
        $this->load->library('logger');
        // 获取教师其他信息
        foreach($logList as &$log) {
            $log['user_type'] = $this->logger->get_user_type_chinese($log['user_type']);
            $log['action_type'] = $this->logger->get_action_type_chinese($log['action_type']);
        }
        // 返回值
        exit(admin_table_data($total, $logList));
    }

    // 插入同步日志
	public function save_sync_log_action() {
        $schoolId = $this->post('school_id');
        $params['schoolId'] = $schoolId;
        $params['isDelete'] = 0;
        $campusList = $this->schoolModel->get_campus_list($params);
        $campusInfo = $campusList[0];
        // 插入日志
        $this->load->library('logger');
        $actionType = $this->logger->get_action_type('sync');
        if($actionType == false) {
            throw new Exception('日志错误');
        }
        $dataArr = array(
            'school_id' => $schoolId,
            'campus_id' => $campusInfo['id'],
            'user_type' => $this->rmxUser['user_type'],
            'user_id' => $this->rmxUser['id'],
            'realname' => $this->rmxUser['realname'] ? $this->rmxUser['realname'] : $this->rmxUser['username'],
            'action_type' => $actionType,
            'content' => '同步数据'
        );
        $this->logger->save_log_action($dataArr);
        
        exit(json('', true));
    }
}